package cn.jingyuan.swan.gw.web.filter;

import cn.jingyuan.swan.gw.constants.RequestConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

/**
 * Servlet filter：前置过滤器
 * <pre>
 *     2、添加请求头请求标识
 *     3、添加请求头请求时间
 * </pre>
 */
@Slf4j
public class HeaderFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(
        HttpServletRequest request,
        HttpServletResponse response,
        FilterChain filterChain
    ) throws ServletException, IOException {
        request.setAttribute(RequestConstants.HEADER_REQUEST_TIME, new Date());

        filterChain.doFilter(request, response);
    }

}
